package com.zzptc.zzsgz.bdsjws.services;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.widget.Toast;

import com.socks.library.KLog;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.FileCallBack;
import com.zzptc.zzsgz.bdsjws.MyApplication;
import com.zzptc.zzsgz.bdsjws.R;
import com.zzptc.zzsgz.bdsjws.activities.UpdateActivity;
import com.zzptc.zzsgz.bdsjws.utils.MyUtil;

import java.io.File;

import okhttp3.Call;

import static android.R.attr.path;

public class MyDownLoadService extends Service {
    NotificationManager notificationManager;
    NotificationCompat.Builder notificationBuild;
    public MyDownLoadService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //Toast.makeText(this, "服务启动了", Toast.LENGTH_SHORT).show();
        String url=null;
        if (intent!=null) {
            url = intent.getStringExtra("apkurl");
        }
        //Toast.makeText(this,url+ "", Toast.LENGTH_SHORT).show();
        //创建通知栏
        createNotification();
        File file= MyUtil.getDir();
        url="http://192.168.1.137:8080/mobileDemo/apk/bdsjws-debug.apk";
        downloadfile(url,file);
        return super.onStartCommand(intent, flags, startId);
        //传APK的地址


    }
    private void downloadfile(String url, final File file){
        OkHttpUtils.get()
                .url(url)
                .build()
                .execute(new FileCallBack(file.getAbsolutePath(),"hhh.apk") {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        notificationBuild.setContentText("下载失败");
                        notificationManager.notify(0,notificationBuild.build());
                    }
                    //安装
                    @Override
                    public void onResponse(File response, int id) {
                        notificationBuild.setContentText("下载完毕");
                        notificationBuild.setOngoing(false);
                        notificationBuild.setAutoCancel(true);
                        notificationManager.notify(id,notificationBuild.build());
                        //installApk(file);
                        Intent intent=MyUtil.getInstallIntent(response);
                        //强制更新
                        startActivity(intent);
                        //普通更新
                      /*  TaskStackBuilder stackBuilder=TaskStackBuilder.create(MyDownLoadService.this);
                        stackBuilder.addParentStack(UpdateActivity.class);
                        stackBuilder.addNextIntent(intent);
                        PendingIntent pendingIntent=
                                stackBuilder.getPendingIntent(
                                        0,
                                        PendingIntent.FLAG_CANCEL_CURRENT
                                );
                        notificationBuild.setContentIntent(pendingIntent);
                        notificationBuild.setProgress(0,0,false);
                        notificationBuild.setContentText("请单击安装");
                        notificationBuild.setContentText("下载完毕");*/
                        notificationManager.notify(0,notificationBuild.build());
                    }
                    public void inProgress(float progress, long total, int id) {
                       // System.out.println("***********"+progress);
                        super.inProgress(progress,total,id);
                        notificationBuild.setProgress(100,(int)(progress*100),false);
                        notificationBuild.setContentText("已经下载了"+(int)(progress*100)+"%");
                        notificationManager.notify(id,notificationBuild.build());
                    }


                });


    }
   /* //安装
    public void installApk(File file){
        Intent intent=new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");
        this.startActivity(intent);

    }*/
    //创建通知栏
    public void createNotification(){
         notificationManager = (NotificationManager) MyApplication.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
        notificationBuild = new NotificationCompat.Builder(MyApplication.getContext());
        notificationBuild.setContentTitle("下载中……")
                .setContentText("准备下载……")
                .setSmallIcon(R.mipmap.logo_small)
                .setOngoing(true) //设置他为一个正在进行的通知
                .setAutoCancel(false);
        notificationManager.notify(0, notificationBuild.build());


    }
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
